package com.labManage.service.impl;

import com.labManage.dao.UserDao;
import com.labManage.dao.UserRoleDao;
import com.labManage.entity.Role;
import com.labManage.entity.User;
import com.labManage.service.UserService;
import com.labManage.util.Md5Util;
import com.labManage.vo.UserVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    //利用@Autowired注解通过Spring配置文件中的组件扫描器获取userDao对象
    @Autowired
    UserDao userDao = null;
    @Autowired
    UserRoleDao userRoleDao;

    /***************************************************************************************************************************
     *  @fn             login
     *  @brief          用户登录service方法
     *  @type           非静态方法
     *  @detail         通过接收controller的参数，进行dao方法的调用
     *  @param[in]      User user   接收controller的信息，包括学号和密码
     *  @return         User        返回登录用户在数据库中的所有信息
     *  @author         杜健华，杜志华
     *  @date           2021.06.18
     ****************************************************************************************************************************/
    @Override
    public User login(UserVo uservo) {
        //先对controller传过来的密码进行指定方式加密
        String md5Password= Md5Util.getMd5Simple(uservo.getPassword());
        uservo.setPassword(md5Password);
        //调用相应的dao方法
        User userInfo = userDao.selectUserByUserNoAndPassword(uservo);
        //如果能够查到，则返回登录用户在数据库的所有信息，返回给controller继续做判断
        if(null!=userInfo){
            return userInfo;
        }
        return null;
    }

    /***************************************************************************************************************************
     *  @fn             register
     *  @brief          用户注册service方法
     *  @type           非静态方法
     *  @detail         通过接收controller的参数，进行dao方法的调用
     *  @param[in]      User user   接收controller的信息，包括姓名、学号、密码、邮箱、手机号
     *  @return         int         返回注册响应状态
     *  @author         杜健华，杜志华
     *  @date           2021.06.18
     ****************************************************************************************************************************/
    @Override
    public int register(User user) {
        User selectUser = new User();
        selectUser.setUserNo(user.getUserNo());
        //调用相应的dao方法，用唯一标识学号进行查找
        List<User> users = userDao.selectUser(selectUser);
        //如果查不到，则进行插入数据库操作
        if(0==users.size()) {
            Date date = new Date();
            user.setModifyPerson(user.getUsername());
            user.setCreatePerson(user.getUsername());
            user.setModifyTime(date);
            user.setCreateTime(date);
            int result = userDao.insertUser(user);
            return result;
        }
        return 0;
    }

    @Override
    public List<UserVo> getAllUser(Integer page,Integer limit,UserVo condition) {
        int startIndex = (page-1)*limit;
        List<UserVo> users = userDao.selectAllUserLimit(startIndex, limit,condition);
        return users;
    }

    @Override
    public int getUserNum(UserVo condition) {
       int num = userDao.selectUserNum(condition);
       return num;
    }

    @Override
    public User getUserInfoById(Long modifyUserId) {
        User user = new User();
        user.setId(modifyUserId);
        List<User> users = userDao.selectUser(user);
        return users.get(0);
    }

    @Override
    public int ModifyUserInfo(User user, List<Long> roles){

        Subject subject = SecurityUtils.getSubject();
        User loginUser = (User)subject.getPrincipal();
        user.setModifyPerson(loginUser.getUsername());
        Date date = new Date();
        user.setModifyTime(date);
        if(null!=user.getPassword()){
            String md5Simple = new Md5Hash(user.getPassword(), "123", 1).toString();
            user.setPassword(md5Simple);
        }
        /*更新用户表用户信息*/
        int result = userDao.updateUser(user);
        /*根据用户ID删除用户所有角色信息*/
        result = userRoleDao.deleteUserRoleInfoByUserId(user);
        /*根据用户ID添加新的角色信息*/
        result = userRoleDao.insertUserRoleInfoByUserId(user,roles);
        return result;
    }

    @Override
    public int deleteUserInfo(Long id) {
        int result = userDao.deleteUserById(id);
        return result;
    }

    @Override
    public int updateUserPassword(Long id, String newPwd,String username) {
        User user = new User();
        user.setId(id);
        user.setPassword(new Md5Hash(newPwd, "123", 1).toString());
        user.setModifyPerson(username);
        Date date = new Date();
        user.setModifyTime(date);
        int result = userDao.updateUser(user);
        return result;
    }

    @Override
    public int initPassword(User user) {
        Subject subject = SecurityUtils.getSubject();
        User loginUser = (User)subject.getPrincipal();
        user.setModifyPerson(loginUser.getUsername());
        Date date = new Date();
        user.setModifyTime(date);
        user.setPassword(new Md5Hash(user.getPassword(), "123", 1).toString());
        int result = userDao.updateUser(user);
        return result;
    }
}
